package com.ita.controller;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ita.pojo.MUser;
import com.ita.pojo.TenderRecord;
import com.ita.service.TenderRecordService;
import com.ita.util.JsonResult;

@RestController
@CrossOrigin
public class ActivityController {

	@Autowired(required = true)
	private TenderRecordService tenderRecordService;
	
	@RequestMapping("/sec/activity/check")
	public JsonResult checkJoin(String mid, String activityId){
		JsonResult result = null;
		TenderRecord tr = tenderRecordService.find(activityId, mid);
		if(tr!=null){
			result = new JsonResult(200, "Join", tr);
		}else{
			result = new JsonResult(200, "No join", null);
		}
		return result;
	}
	
	@RequestMapping("/sec/activity/join")
	public JsonResult addJoin(@RequestParam("activity_id")String activityId,Double amount,
			String activity_name, String activity_desc,
			HttpSession session){
		JsonResult result = null;
		MUser m = (MUser)session.getAttribute("m");
		TenderRecord record = new TenderRecord();
		record.setActivity_id(activityId);
		record.setAmount(amount);
		record.setCreate_time(new Date().getTime());
		record.setId(UUID.randomUUID().toString());
		record.setM_id(m.getId());
		record.setState(0);
		record.setActivity_name(activity_name);
		record.setActivity_desc(activity_desc);
		TenderRecord newRecord = tenderRecordService.add(record);
		if(newRecord!=null){
			result = new JsonResult(200, "success", newRecord);
		}else{
			result = new JsonResult(400, "failure", null);
		}
		return result;
	}

	@RequestMapping("/sec/activity/record")
	public JsonResult findRecord(HttpSession session){
		MUser m = (MUser)session.getAttribute("m");
		if(m!=null){
			return new JsonResult(200, "success", tenderRecordService.findAll(m.getId()));	
		}
		return new JsonResult(400, "failure", null);
	}
}
